//
//  MyTicketsListViewController.m
//  HuiMWorld
//
//  Created by chirui on 2018/1/9.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "MyTicketsListViewController.h"
#import "TicketsTableViewCell.h"

@interface MyTicketsListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSInteger buttonCurryIndex;
    UITableView * _tableView;
    UIView * lineView;
    NSArray * _dataArray;
    NSString * staticeStr;
    UIView * noBodyView;
}

@end

@implementation MyTicketsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    buttonCurryIndex = 0;
    staticeStr = @"1";
    [self creatTableView];
    TitleView(@"优惠券");
    [self getTicketsListDataForstatus:staticeStr];
     noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-150, 200, 200) andMessageStr:@"没有相关内容！" onView:nil];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SH_TopHeight) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor =[UIColor groupTableViewBackgroundColor];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[TicketsTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[TicketsTableViewCell reuseIdentifier]];
    
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getTicketsListDataForstatus:staticeStr];
    }];
    _tableView.mj_header = header;
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 105;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 52;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    
    TicketsTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[TicketsTableViewCell reuseIdentifier] forIndexPath:indexPath];
    cell.priceLabel.text = dic[@"price"];
    cell.detailsLabel.text = [NSString stringWithFormat:@"%@可用",dic[@"describe"]];
    cell.nameLbel.text = dic[@"type_string"];
    cell.timelabel.text = [NSString stringWithFormat:@"%@至%@",dic[@"create_time"],dic[@"expiration_time"]];

    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH,52)];
    view.backgroundColor = [UIColor whiteColor];
    
    NSArray * titleArray = @[@"未使用",@"已使用",@"已过期"];
    for (int i = 0; i<3; i++) {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(WIDTH/3 * i, 0, WIDTH/3, 50);
        btn.tag = 1000+i;
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.backgroundColor =[UIColor clearColor];
        if (i==buttonCurryIndex) {
            [btn setTitleColor:MyTabbarClick forState:UIControlStateNormal];

              lineView =[[UIView alloc]initWithFrame:CGRectMake(WIDTH/3*buttonCurryIndex, 50, WIDTH/3-40, 2)];
                        lineView.backgroundColor = MyTabbarClick;
             lineView.center = CGPointMake(btn.center.x, 51);
                        [view addSubview:lineView];
        }
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
    }
   
    return view;
    
    
}

-(void)btnClick:(UIButton *)btn{
    
    NSInteger i = btn.tag - 1000;
    if (buttonCurryIndex==i) {
        return;
    }
    UIButton * button = (id)[self.view viewWithTag:buttonCurryIndex+1000];
    [button setTitleColor:MyTabbarClick forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   
    lineView.center= CGPointMake(btn.center.x, 50);
    buttonCurryIndex = i;
    NSLog(@"btttonCurryIndex == %ld",buttonCurryIndex);
    if (buttonCurryIndex ==0) {
        staticeStr = @"1";
    }
    if (buttonCurryIndex ==1) {
       staticeStr = @"2";
//         [_tableView reloadData];
    }
    if (buttonCurryIndex ==2) {
        staticeStr = @"3";
//         [_tableView reloadData];
    }
    [self getTicketsListDataForstatus:staticeStr];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

-(void)getTicketsListDataForstatus:(NSString *)status{
    
//    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
//                           @"token":USER_DEFAULTGet(@"token"),
//                           @"status":status
//                           };
//
//    SHuiMBhubNoTimeStr(@"正在加载...");
//    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_couponList_api) andparameters:dic Success:^(id responseObject) {
//        SHuiMBHidleYes;
//        if (codeSucess) {
//
//           if ([responseObject[@"datas"]  isKindOfClass:[NSArray class]]) {
//
//               _dataArray = responseObject[@"datas"];
//           }else {
//
//           }
//            dispatch_async(dispatch_get_main_queue(), ^{
//
//                if (_dataArray.count>0) {
//                    noBodyView.hidden =YES;
//                }else {
//
//                    noBodyView.hidden =NO;
//                    [self.view addSubview:noBodyView];
//                }
//                [_tableView.mj_header endRefreshing];
//                [_tableView reloadData];
//            });
//        }else {
//
//            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
//        }
//    } failed:^(NSError *error) {
//        SHuiMBHidleYes;
//        SHuiMBhudTextTimeStr(@"网络错误请重试！");
//    }];
    
}


@end
